using System;
using System.Collections.Generic;
using System.Text;
using Buka.BLL.Model;

namespace Buka.BLL.Provider
{

    [System.ComponentModel.DataObject()]
    public class MasterProvider
    {
        public MasterProvider()
        {
            //return null;
        }
        Buka.DAL.MastersTableAdapters.MastersTableAdapter _masterTableAdapter = new Buka.DAL.MastersTableAdapters.MastersTableAdapter();
        #region "DB Methods"

        [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select, true)]
        public List<Master> GetAll()
        {
            List<Master> masters = new List<Master>();
            foreach (Buka.DAL.Masters.MastersRow row in _masterTableAdapter.GetData())
            {
                Master master = GetObjectFromDataRow(row);
                masters.Add(master);
            }
            return masters;
        }

        #endregion

        #region "Fill & Get"

        private void FillObjectFromDataRow(Buka.DAL.Masters.MastersRow row, Buka.BLL.Model.Master obj)
        {
            obj.Id = row.id;
            obj.Caption = row.caption;
            obj.Description = row.description;
            if (row.IsurlNull())
            {
                obj.Url = "";
            }
            else
            {
                obj.Url = row.url;
            }
            if (row.Isshort_textNull())
            {
                obj.ShortText = "";
            }
            else
            {
                obj.ShortText = row.short_text;
            }
            if (row.IstextNull())
            {
                obj.Text = "";
            }
            else
            {
                obj.Text = row.text;
            }
            if (row.Isimage_pathNull())
            {
                obj.ImagePath = "";
            }
            else
            {
                obj.ImagePath = row.image_path;
            }
            if (row.IsrankingNull())
            {
                obj.Ranking = null;
            }
            else
            {
                obj.Ranking = row.ranking;
            }
            if (row.Iscategory_idNull())
            {
                obj.CategoryId = null;
            }
            else
            {
                obj.CategoryId = row.category_id;
            }
            if (row.Istype_idNull())
            {
                obj.TypeId = row.type_id;
            }
            if (row.Iscreated_byNull())
            {
                obj.CreatedBy = null;
            }
            else
            {
                obj.CreatedBy = row.created_by;
            }
            if (row.Iscreated_dateNull())
            {
                obj.CreatedDate = null;
            }
            else
            {
                obj.CreatedDate = row.created_date;
            }
        }

        private Buka.BLL.Model.Master GetObjectFromDataRow(Buka.DAL.Masters.MastersRow row)
        {
            Master master = new Master();
            try
            {
                FillObjectFromDataRow(row, master);
            }
            catch (Exception ex)
            {
                throw new ArgumentException(ex.Message);
            }
            return master;
        }

        #endregion
    }
}